/**
 * Description:
 * Date: 2025-10-22
 * Time: 19:23
 */
public class Practice {

    //字符串中第一个只出现一次的字符
    public static void main1(String[] args) {
        String s = "loveleetcode";
        /*int[] array = new int[200];
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            array[ch]++;
        }

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(array[ch] == 1){
                System.out.println(i);
                return;
            }
        }*/

        //第一次遍历字符串获得每个字符出现的次数
        int[] arr = new int[26];
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            arr[ch - 'a']++;
        }

        //第二次遍历字符串获得第一个出现次数为1的字符
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(arr[ch - 'a'] == 1){
                System.out.println(i);
                break;
            }
        }

    }

    //字符串最后一个单词的长度
    public static void main2(String[] args) {
        String s = "HelloNowcoder";
        /*int len = s.length();
        int pos = 0;
        int i = len - 1;
        for (; i >=0 ; i--) {
            char ch = s.charAt(i);
            if(ch == ' '){
                break;
            }
        }
        System.out.println(len - i - 1);*/

        //以空格分隔
        /*String[] str = s.split(" ");
        int len = str.length;
        int ret = str[str.length - 1].length();
        System.out.println(ret);*/

        //截取字符串
        int index = s.lastIndexOf(" ") + 1;
        String s1 = s.substring(index);
        System.out.println(s1.length());
    }

    public static boolean func(char ch) {
        //判断当前字符是否时数字或字母字符
        if(Character.isDigit(ch) || Character.isLetter(ch)){
            return  true;
        }
        return false;
    }

    //验证回文串
    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";

        //大写字母转换成小写字母
        s = s.toLowerCase();

        int left = 0;
        int right = s.length() - 1;
        //依靠当前不正确来推进代码的执行
        while (left < right) {
            //从左往右找数字字母字符,找到就对比，找不到left就往左走
            while (left < right && !func(s.charAt(left))){
                left++;
            }

            while (left < right && !func(s.charAt(right))){
                right--;
            }

            if(s.charAt(left) != s.charAt(right)){
                System.out.println(false);
            }
            left++;
            right--;
        }
        System.out.println(true);


    }


}
